﻿using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using KeRui.Common;
using System.IO;

public partial class Manage_Cms_TemplateItem : System.Web.UI.Page
{
    protected KeRui.BLL.CmsTemplate bllCmsTemplate = new KeRui.BLL.CmsTemplate();
    protected KeRui.Model.CmsTemplate modelCmsTemplate = new KeRui.Model.CmsTemplate();
    private ManageErrMsg nsb = new ManageErrMsg();
    protected string ActFlag = "addsave";
    protected string ActName = "添加";
    protected KeRui.BLL.CmsServer bllCmsServer = new KeRui.BLL.CmsServer();

    protected KeRui.BLL.CmsNode bllCmsNode = new KeRui.BLL.CmsNode();
    protected void Page_Load(object sender, EventArgs e)
    {

        ManageHelper.CheckAdminLogin();//登陆判断
        //权限
        ManageHelper.CheckAdminPower("system_templatelist");
        switch (ManageHelper.PageAct())
        {
            case "addsave":
                Add();
                break;
            case "edit":
                Edit();
                break;
            case "editsave":
                EditSave();
                break;
            case "del":
                Del();
                break;
            default:
                break;
        }
    }

    #region 添加
    private void Add()
    {
        //添加权限
        ManageHelper.CheckAdminPower("system_templatelistadd");
        modelCmsTemplate.TemplateName = PageRequest.GetString("TemplateName");
        if (modelCmsTemplate.TemplateName == "")
        {
            nsb.AddErr("请输入模板名称。");
        }
        modelCmsTemplate.TemplateFolder = PageRequest.GetString("TemplateFolder");
        if (modelCmsTemplate.TemplateFolder == "")
        {
            nsb.AddErr("请输入文件夹名称。");
        }
        modelCmsTemplate.TemplateDir = PageRequest.GetString("TemplateDir");
        if (modelCmsTemplate.TemplateDir == "")
        {
            nsb.AddErr("请输入文件夹路径。");
        }
        modelCmsTemplate.ServerID = PageRequest.GetInt("ServerID", -1);
        if (modelCmsTemplate.ServerID == -1)
        {
            nsb.AddErr("请选择服务器。");
        }
        nsb.ChkErr();
        
        int retInt = bllCmsTemplate.Add(modelCmsTemplate);
        if (retInt == 1)
        {
            nsb.AddErr("已有相同的模型名称。");
        }
        else
        {
            KeRui.Common.FileObj.FolderCreate(modelCmsTemplate.TemplateDir, modelCmsTemplate.TemplateFolder);

            //添加日志
            ManageHelper.AddLog("system_templatelistadd", "添加了模板" + modelCmsTemplate.TemplateName);
            Response.Write("<script type=\"text/javascript\">window.parent.location.href=\"TemplateList.aspx?sucmsg=添加成功。\";</script>");
        }
    }
    #endregion

    #region 反显修改页面
    private void Edit()
    {
        ActFlag = "editsave";
        ActName = "修改";
        int TemplateID = PageRequest.GetInt("ID", -1);
        if (TemplateID == -1)
        {
            nsb.AddErr("该采集规则不存在。");
            nsb.ChkErr();
        }
        modelCmsTemplate = bllCmsTemplate.GetModel(TemplateID);
    }
    #endregion

    #region 修改时保存
    private void EditSave()
    {
        //修改权限
        ManageHelper.CheckAdminPower("system_templatelistupdate");
        modelCmsTemplate.TemplateID = PageRequest.GetInt("TemplateID", -1);
        modelCmsTemplate.TemplateName = PageRequest.GetString("TemplateName");
        if (modelCmsTemplate.TemplateName == "")
        {
            nsb.AddErr("请输入模板名称。");
        }
        modelCmsTemplate.TemplateFolder = PageRequest.GetString("TemplateFolder");
        if (modelCmsTemplate.TemplateFolder == "")
        {
            nsb.AddErr("请输入文件夹名称。");
        }
        modelCmsTemplate.TemplateDir = PageRequest.GetString("TemplateDir");
        if (modelCmsTemplate.TemplateDir == "")
        {
            nsb.AddErr("请输入文件夹路径。");
        }
        modelCmsTemplate.ServerID = PageRequest.GetInt("ServerID", -1);
        if (modelCmsTemplate.ServerID == -1)
        {
            nsb.AddErr("请选择服务器。");
        } nsb.ChkErr();

        KeRui.Model.CmsTemplate modelOld = bllCmsTemplate.GetModel(modelCmsTemplate.TemplateID);

        int RetFlag = bllCmsTemplate.Update(modelCmsTemplate);
        if (RetFlag == 1)
        {
            nsb.AddErr("对不起！已存在相同的文件夹。");
            nsb.ChkErr();
        }
        else
        {
            KeRui.Common.FileObj.DeleteFolder(modelOld.TemplateDir + "//" + modelOld.TemplateFolder);

            KeRui.Common.FileObj.FolderCreate(modelCmsTemplate.TemplateDir, modelCmsTemplate.TemplateFolder);

            //修改日志
            ManageHelper.AddLog("system_templatelistupdate", "修改了模板" + modelCmsTemplate.TemplateName); 
            Response.Write("<script type=\"text/javascript\">window.parent.location.href=\"TemplateList.aspx?sucmsg=修改成功。\";</script>");
        }
    }
    #endregion

    #region 删除
    private void Del()
    {
        //删除权限
        ManageHelper.CheckAdminPower("system_templatelistdelete");
        int TemplateID = PageRequest.GetInt("ID", -1);
        if (TemplateID == -1)
        {
            nsb.AddErr("错误访问。");
            nsb.ChkErr();
        }
        modelCmsTemplate = bllCmsTemplate.GetModel(TemplateID);
        if (modelCmsTemplate == null)
        {
            nsb.AddErr("该模板不存在。");
        }
        else {
            KeRui.Common.FileObj.DeleteFolder(modelCmsTemplate.TemplateFolder);
        }

        if (bllCmsTemplate.Delete(TemplateID)>0)
        {   
            //删除的日志
            ManageHelper.AddLog("system_templatelistdelete", "删除了模板，ID为" + TemplateID);
            Response.Redirect("TemplateList.aspx?sucmsg=恭喜。删除成功。");
        }
        else
        {
            Response.Redirect("TemplateList.aspx?sucmsg=抱歉。删除失败。原因：该模板已被使用。");
        }
    }
    #endregion
}
